package com.zhiyou.crud.service.impl;

import com.zhiyou.crud.bean.*;
import com.zhiyou.crud.dao.*;
import com.zhiyou.crud.service.UserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.util.List;

@Service
public class  UserCenterServiceImpl implements UserCenterService {

    @Autowired
    ProjectMapper projectMapper;
    @Autowired
    DonateMapper donateMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    LetterMapper letterMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    RealMapper realMapper;

    /*
    * 查看我发起的所有项目
    * */
    @Override
    public List<Project> getIStartProject(Integer uno) {
        Example example=new Example(Project.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uno", uno);
        return projectMapper.selectByExample(example);
    }

    /*
     * 查看我发起的所有众筹中的项目
     * */
    @Override
    public List<Project> getIStartProject2(Integer uno) {
        Example example=new Example(Project.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uno", uno).andEqualTo("pstatus", 4);
        return projectMapper.selectByExample(example);
    }

    /*
    * 查看我参与的所有项目
    * */
    @Override
    public List<Donate> getIjoinProject(Integer uno) {
        Example example=new Example(Donate.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uno", uno);
        List<Donate> donates = donateMapper.selectByExample(example);

        for(int i=0;i<donates.size();i++){
            Example example2=new Example(Project.class);
            Example.Criteria criteria2 = example2.createCriteria();
            criteria2.andEqualTo("pno",donates.get(i).getPno());
            Project project = projectMapper.selectOneByExample(example2);
            donates.get(i).setProject(project);
        }
        return donates;
    }

    /*
     * 查看我的所有评论
     * */
    @Override
    public List<Comment> getMyComment(Integer uno) {
        Example example=new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uno", uno);
        List<Comment> comments =commentMapper.selectByExample(example);

        for(int i=0;i<comments.size();i++){
            Example example2=new Example(Project.class);
            Example.Criteria criteria2 = example2.createCriteria();
            criteria2.andEqualTo("pno",comments.get(i).getPno());
            Project project = projectMapper.selectOneByExample(example2);
            comments.get(i).setProject(project);
        }
        return comments;
    }

    /*
     * 查询我的私信/通知
     * */
    @Override
    public List<Letter> getMyInform(Integer uno) {
        Example example=new Example(Letter.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("lrno", uno);
        List<Letter> letters =letterMapper.selectByExample(example);
        return letters;
    }

    /*
     * 查询我的个人资料
     * */
    @Override
    public User getMyData(Integer uno) {
        User user = userMapper.selectByPrimaryKey(uno);
        return user;
    }

    /*
     * 实名认证
     * */
    //提交实名认证 AND 更改更改个人信息状态
    @Override
    public boolean userAutonymAndstatus(Real real) {
        int bbb=realMapper.insertSelective(real);
        if(bbb>0){
            Example example=new Example(User.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("uno", real.getUno()).andEqualTo("realstatus", 1);
            User user=new User();
            user.setUno(real.getUno());
            user.setRealstatus(1);
            int aaa=userMapper.updateByPrimaryKeySelective(user);
            if(aaa>0){
                return true;
            }
        }
        return false;
    }

    /*
     * 修改密码/资料修改
     * */
    @Override
    public boolean updateUserPassword(User user) {
        int bbb=userMapper.updateByPrimaryKeySelective(user);
        return bbb>0?true:false;
    }
}
